home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-13 / joe014.zip / JOE014.TAZ / JOE014.tar / asynclinux.c < prev    next >
C/C++ Source or Header  |  1992-01-23  |  5KB  |  285 lines

  1. /* Terminal interface for POSIX
  2.    Copyright (C) 1991 Joseph H. Allen
  3.    (Contributed by Mike Lijewski)
  4.  
  5. This file is part of JOE (Joe's Own Editor)
  6.  
  7. JOE is free software; you can redistribute it and/or modify it under the terms
  8. of the GNU General Public License as published by the Free Software
  9. Foundation; either version 1, or (at your option) any later version.  
  10.  
  11. JOE is distributed in the hope that it will be useful, but WITHOUT ANY
  12. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  13. A PARTICULAR PURPOSE.  See the GNU General Public License for more details.  
  14.  
  15. You should have received a copy of the GNU General Public License along with
  16. JOE; see the file COPYING.  If not, write to the Free Software Foundation, 675
  17. Mass Ave, Cambridge, MA 02139, USA.  */ 
  18.  
  19. #include <stdio.h>
  20. #include <signal.h>
  21. #include <fcntl.h>
  22. #include <sys/time.h>
  23. #include <termios.h>
  24. #include <unistd.h>
  25. #include "async.h"
  26.  
  27. void tsignal();
  28. #define DIVISOR 12000000
  29. #define TIMES 2
  30.  
  31. static struct termios oldterm;
  32.  
  33. static unsigned char *obuf=0;
  34. static unsigned obufp=0;
  35. static unsigned obufsiz;
  36. static unsigned long ccc;
  37.  
  38. static speed_t speeds[]=
  39. {
  40. B50,50,B75,75,B110,110,B134,134,B150,150,B200,200,B300,300,B600,600,B1200,1200,
  41. B1800,1800,B2400,2400,B4800,4800,B9600,9600,EXTA,19200,EXTB,38400,B19200,19200,
  42. B38400,38400
  43. };
  44.  
  45. sigjoe()
  46. {
  47. signal(SIGHUP,tsignal);
  48. signal(SIGTERM,tsignal);
  49. signal(SIGPIPE,SIG_IGN);
  50. signal(SIGINT,SIG_IGN);
  51. signal(SIGQUIT,SIG_IGN);
  52. }
  53.  
  54. signorm()
  55. {
  56. signal(SIGHUP,SIG_DFL);
  57. signal(SIGTERM,SIG_DFL);
  58. signal(SIGQUIT,SIG_DFL);
  59. signal(SIGPIPE,SIG_DFL);
  60. signal(SIGINT,SIG_DFL);
  61. }
  62.  
  63. aopen()
  64. {
  65. int x;
  66. speed_t baud;
  67. struct termios newterm;
  68. fflush(stdout);
  69. /* tcdrain(STDOUT_FILENO); */
  70. tcgetattr(STDIN_FILENO,&oldterm);
  71. newterm=oldterm;
  72. newterm.c_lflag&=0;
  73. newterm.c_iflag&=~(ICRNL|IGNCR|INLCR);
  74. newterm.c_oflag&=0;
  75. newterm.c_cc[VMIN]=1;
  76. newterm.c_cc[VTIME]=0;
  77. tcsetattr(STDIN_FILENO,TCSANOW,&newterm);
  78. ccc=0;
  79. baud=cfgetospeed(&newterm);
  80. for(x=0;x!=34;x+=2)
  81.  if(baud==speeds[x])
  82.   {
  83.   ccc=DIVISOR/speeds[x+1];
  84.   break;
  85.   }
  86. if(obuf) free(obuf);
  87. if(!(TIMES*ccc)) obufsiz=4096;
  88. else
  89.  {
  90.  obufsiz=1000000/(TIMES*ccc);
  91.  if(obufsiz>4096) obufsiz=4096;
  92.  }
  93. if(!obufsiz) obufsiz=1;
  94. obuf=(unsigned char *)malloc(obufsiz);
  95. }
  96.  
  97. aclose()
  98. {
  99. aflush();
  100. tcsetattr(STDIN_FILENO,TCSANOW,&oldterm);
  101. }
  102.  
  103. int have=0;
  104. static unsigned char havec;
  105. static int yep;
  106.  
  107. static void dosig()
  108. {
  109. yep=1;
  110. }
  111.  
  112. aflush()
  113. {
  114. if(obufp)
  115.  {
  116.  unsigned long usec=obufp*ccc;
  117.  if(usec>=500000/10  /* HZ */)
  118.   {
  119.   /* well there's no point in doing this since...
  120.   struct timeval timeval;
  121.   timeval.tv_sec=usec/1000000;
  122.   timeval.tv_usec=usec%1000000; */
  123.   write(fileno(stdout),obuf,obufp);
  124.   /*
  125.   select(0,0,0,0,&timeval); */
  126.   }
  127.  else write(fileno(stdout),obuf,obufp);
  128.  obufp=0;
  129.  }
  130. if(!have)
  131.  {/* this doesn't work
  132.  fcntl(STDIN_FILENO,F_SETFL,O_NDELAY);
  133.  if(read(STDIN_FILENO,&havec,1)==1) have=1;
  134.  fcntl(STDIN_FILENO,F_SETFL,0); */
  135.  }
  136. }
  137.  
  138. unsigned char *take=0;
  139.  
  140. anext()
  141. {
  142. if(take)
  143.  if(*take)
  144.   {
  145.   int c;
  146.   if(*take!='\\') return *take++;
  147.   ++take;
  148.   if(!*take) return '\\';
  149.   else if(*take=='r') c='\r';
  150.   else if(*take=='b') c=8;
  151.   else if(*take=='n') c=10;
  152.   else if(*take=='f') c=12;
  153.   else if(*take=='a') c=7;
  154.   else if(*take=='\"') c='\"';
  155.   else if(*take>='0' && *take<='7')
  156.         {
  157.         c= *take++-'0';
  158.         if(*take>='0' && *take<='7')
  159.          {
  160.          c=c*8+*take++-'0';
  161.          if(*take>='0' && *take<='7') c=c*8+*take++-'0';
  162.          }
  163.         --take;
  164.         }
  165.   else c= *take;
  166.   ++take;
  167.   return c;
  168.   }
  169.  else take=0;
  170. aflush();
  171. if(have) have=0;
  172. else if(read(STDIN_FILENO,&havec,1)<1) tsignal(0);
  173. if(record) macroadd(havec);
  174. return havec;
  175. }
  176.  
  177. eputc(c)
  178. unsigned char c;
  179. {
  180. obuf[obufp++]=c;
  181. if(obufp==obufsiz) aflush();
  182. }
  183.  
  184. eputs(s)
  185. char *s;
  186. {
  187. while(*s)
  188.  {
  189.  obuf[obufp++]= *(s++);
  190.  if(obufp==obufsiz) aflush();
  191.  }
  192. }
  193.  
  194. getsize()
  195. {
  196. #ifdef TIOCGSIZE
  197. struct ttysize getit;
  198. #else
  199. #ifdef TIOCGWINSZ
  200. struct winsize getit;
  201. #else
  202. char *p;
  203. #endif
  204. #endif
  205. #ifdef TIOCGSIZE
  206. if(ioctl(fileno(stdout),TIOCGSIZE,&getit)!= -1)
  207.  {
  208.  if(getit.ts_lines>=3) height=getit.ts_lines;
  209.  if(getit.ts_cols>=2) width=getit.ts_cols;
  210.  }
  211. #else
  212. #ifdef TIOCGWINSZ
  213. if(ioctl(fileno(stdout),TIOCGWINSZ,&getit)!= -1)
  214.  {
  215.  if(getit.ws_row>=3) height=getit.ws_row;
  216.  if(getit.ws_col>=2) width=getit.ws_col;
  217.  }
  218. #else
  219. if(p=getenv("ROWS")) sscanf(p,"%d",&height);
  220. if(p=getenv("COLS")) sscanf(p,"%d",&width);
  221. if(height<3) height=24;
  222. if(width<2) width=80;
  223. #endif
  224. #endif
  225. }
  226.  
  227. termtype()
  228. {
  229. unsigned char entry[1024];
  230. unsigned char area[1024];
  231. unsigned char *foo=area;
  232. unsigned char *x=(unsigned char *)getenv("TERM");
  233. if(!x) goto down;
  234. if(tgetent(entry,x)!=1) goto down;
  235. height=tgetnum("li");
  236. if(height<3) height=24;
  237. width=tgetnum("co");
  238. if(width<2) width=80;
  239. if(!tgetstr("cs",&foo)) scroll=0;
  240. down:
  241. getsize();
  242. }
  243.  
  244. shell()
  245. {
  246. int x;
  247. char *s=(char *)getenv("SHELL");
  248. if(!s)
  249.  {
  250.  puts("\nSHELL variable not set");
  251.  return;
  252.  }
  253. eputs("\nYou are at the command shell.  Type 'exit' to continue editing\r\n");
  254. aclose();
  255. if(x=fork())
  256.  {
  257.  if(x!= -1) wait(0);
  258.  }
  259. else
  260.  {
  261.  signorm();
  262.  execl(s,s,0);
  263.  _exit(0);
  264.  }
  265. aopen();
  266. }
  267.  
  268. susp()
  269. {
  270. #ifdef SIGCONT
  271. eputs("\nThe editor has been suspended.  Type 'fg' to continue editing\r\n");
  272. yep=0;
  273. aclose();
  274. signal(SIGCONT,dosig);
  275. /* sigsetmask(sigmask(SIGCONT)); */
  276. kill(0,SIGTSTP);
  277. /* while(!yep) sigpause(0); */
  278. while(!yep) pause();
  279. signal(SIGCONT,SIG_DFL);
  280. aopen();
  281. #else
  282. shell();
  283. #endif
  284. }
  285.